package implementation.gui;

import java.io.IOException;
import java.util.Collection;

import javax.swing.JOptionPane;

import implementation.client.IUI;
import implementation.client.IUserInput;
import implementation.gui.screens.Screen;
import implementation.gui.screens.Show;
import implementation.gui.screens.ShowAddThreadScreen;
import implementation.gui.screens.ShowDisplayPostScreen;
import implementation.gui.screens.ShowForumsScreen;
import implementation.gui.screens.ShowForumScreen;
import implementation.gui.screens.ShowFriendsScreen;
import implementation.gui.screens.ShowLoginScreen;
import implementation.gui.screens.ShowMainScreen;
import implementation.gui.screens.ShowProfileScreen;
import implementation.gui.screens.ShowRegisterScreen;
import implementation.gui.screens.ShowThreadScreen;
import implementation.gui.screens.ShowAddPostScreen;
import implementation.gui.screens.addFriend;
import implementation.gui.screens.removeFriendScreen;


import implementation.shared.INotification;
import implementation.shared.stubs.IForumStub;
import implementation.shared.stubs.IPostStub;
import implementation.shared.stubs.IThreadStub;
import implementation.shared.stubs.IUserStub;

public class GUIController implements IUI {

    private Object _monitor;

    public GUIController() {
        this._monitor = new Object();
    }

    @Override
    public void showMainScreen(IUserInput userInput) throws IOException {


        ShowMainScreen mainScreen = new ShowMainScreen(userInput, this._monitor);

        display(mainScreen);

    }

    @Override
    public void showProfile(IUserInput userInput) {

        ShowProfileScreen profileScreen = new ShowProfileScreen(userInput, this._monitor);

        display(profileScreen);
    }

    @Override
    public void showLogin(IUserInput userInput) throws IOException {

        ShowLoginScreen loginScreen = new ShowLoginScreen(userInput, this._monitor);

        display(loginScreen);

    }

    @Override
    public void showRegister(IUserInput userInput) throws IOException {

        ShowRegisterScreen registerScreen = new ShowRegisterScreen(userInput, this._monitor);

        display(registerScreen);

    }

    @Override
    public void showForumList(IUserInput userInput,
            Collection<IForumStub> forums) {

        ShowForumsScreen forumsScreen = new ShowForumsScreen(userInput, this._monitor, forums);

        display(forumsScreen);

    }

    @Override
    public void showForum(IUserInput userInput,
            Collection<IThreadStub> threads) throws IOException {

        ShowForumScreen forumScreen = new ShowForumScreen(userInput, this._monitor, threads);

        display(forumScreen);

    }

    @Override
    public void showThread(IUserInput userInput,
            Collection<IPostStub> posts) {


        ShowThreadScreen threadScreen = new ShowThreadScreen(userInput, this._monitor, posts);

        display(threadScreen);

    }

    @Override
    public void showPost(IUserInput userInput, IPostStub post) {

        ShowDisplayPostScreen displayPostScreen = new ShowDisplayPostScreen(userInput, this._monitor, post);

        display(displayPostScreen);

    }

    @Override
    public void showAddPost(IUserInput userInput) throws IOException {

        ShowAddPostScreen addPostScreen = new ShowAddPostScreen(userInput, this._monitor);

        display(addPostScreen);
    }

    @Override
    public void showAddThread(IUserInput userInput) throws IOException {

        ShowAddThreadScreen addThreadScreen = new ShowAddThreadScreen(userInput, this._monitor);

        display(addThreadScreen);

    }

    @Override
    public void showAddFriend(IUserInput userInput) throws IOException {
        Screen addFriendScreen = new addFriend(userInput, this._monitor);
        Show s = new Show(addFriendScreen);
        display(s);
    }

    @Override
    public void showRemoveFriend(IUserInput userInput, Collection<IUserStub> friends) throws IOException {
        Screen removeFriendScreen = new removeFriendScreen(userInput, _monitor, friends);
        Show s = new Show(removeFriendScreen);
        display(s);
    }

    @Override
    public void showFriends(IUserInput userInput,Collection<IUserStub> userList) throws IOException {
        ShowFriendsScreen friendsScreen = new ShowFriendsScreen(userInput, this._monitor, userList);
        display(friendsScreen);


    }

    @Override
    public void showClientSideError(String error) {
        JOptionPane.showMessageDialog(null, error, null, JOptionPane.ERROR_MESSAGE);

    }

    @Override
    public void showClientSideMessage(String msg) {
        JOptionPane.showMessageDialog(null, msg, null, JOptionPane.INFORMATION_MESSAGE);

    }

    @Override
    public void showUserChoise() {
        // TODO Auto-generated method stub
    }

    private void display(Runnable screen) {

        Thread t = new Thread(screen);

        t.start();

        synchronized (this._monitor) {
            try {
                this._monitor.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
    }

    @Override
    public void showNotification(INotification n) {
        String msg = n.getAuthorName() + " has added a new post on " + n.getThreadName() + " in " + n.getForumName();
        JOptionPane.showMessageDialog(null, msg, null, JOptionPane.INFORMATION_MESSAGE);
    }
}
